package com.example.layui_project.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.layui_project.comm.action.BaseController;
import com.example.layui_project.dto.JsonData;
import com.example.layui_project.dto.TableParams;
import com.example.layui_project.dto.TableResult;
import com.example.layui_project.entity.SysRole;
import com.example.layui_project.enums.ResultCode;
import com.example.layui_project.service.PermissionService;
import com.example.layui_project.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("dataAnalysis")
public class DataAnalysisController extends BaseController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private HttpServletRequest request;
    @GetMapping("toDataAnalysisList.do")
    public String toRoleList(){
        return "forms/dataAnalysis";
    }

    @RequestMapping("findData.do")
    @ResponseBody
    public TableResult findData(int page, int limit,String rolename){
        TableParams tableParams = new TableParams();
        HashMap<String, Object> ps = new HashMap<>();
        ps.put("rolename",rolename);
        tableParams.setPs(ps);
        tableParams.setPageNum(page-1);
        tableParams.setPageSize(limit);
        TableResult<SysRole> sysUserTableResult = roleService.find(tableParams);
        sysUserTableResult.setCode(0);
        return sysUserTableResult;
    }

    @PostMapping("add.do")
    @ResponseBody
    public JsonData add(String role,String ids){
        SysRole sysRole = JSON.parseObject(role, SysRole.class);
        try {
            boolean add = roleService.add(sysRole,ids);
            if(add){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("del.do")
    @ResponseBody
    public JsonData del(Integer id){
        try {
            boolean del = roleService.del(id);
            if(del){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("update.do")
    @ResponseBody
    public JsonData update(String role,String ids){
        SysRole sysRole = JSON.parseObject(role, SysRole.class);
        try {
            boolean update = roleService.update(sysRole,ids);
            if(update){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @GetMapping("getPermissionTree.do")
    @ResponseBody
    public JsonData getPermissionTree(Integer roleId){
        try{
            JSONArray objects = permissionService.permissionTree(roleId);
            return JsonData.success(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getInfo(),objects);
        }catch(Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("getAllRole.do")
    @ResponseBody
    public JsonData getAllRole(){
        try{
            List<SysRole> allRole = roleService.getAllRole();
            return JsonData.success(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getInfo(),allRole);
        }catch(Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }
}
